// 定义与main.css对应的不同样式，每新增一个系统都要维护
var syscss = ['vom', 'dbo', 'upmp', 'pmp', 'ets', 'rvau', 'epccb', 'dss', 'vewms', 'dibs', 'sfck', 'ibps'];

$(document).ready(function () {

  getUsersys($("#userId").attr("val"));

  $('#logout-btn').on('click', function () {
    $.post("logout", function (data) {
      window.location.href = $('#contextPath').attr('val');
    });
  });

  $("#logout-btn").hover(function () {
    $("#logout-btn").attr("src", "images/f-exit-press.png");
  }, function () {
    $("#logout-btn").attr("src", "images/f-exit.png");
  });

  resetWinHeight();

  window.onresize = function () {
    resetWinHeight();
  }

});

function getUsersys(userId) {
  Ajax('sys', '/list/' + userId, 'GET', false, null, function (data) {

    var availableSys = "";

    for (var i = 0; i < data.vo.length; i++) {
      var syscss = getSyscss(data.vo[i].sysEnname);
      availableSys += '<a onclick="gotosubsys(\'' + data.vo[i].sysAddress + '\')" class="codelab-card auth" data-wait-for-ripple>';
      availableSys += '<div class="card-header ' + syscss + '_BG">';
      availableSys += '<div class="category-icon ' + syscss + '_ICON"></div>';
      availableSys += '<span class="go-text">' + data.vo[i].sysName + '</span></div>';
      availableSys += '<div class="description">' + data.vo[i].sysDesc + '</div>';
      availableSys += '<div class="card-footer">';
      availableSys += '<button class="goin-btn"></button>';
      availableSys += '<div><div class="card-updated">Version ' + data.vo[i].sysVersion + '</div></div>';
      availableSys += '</div><paper-ripple></paper-ripple></a>';
    }

    var unableSys = "";

    for (var i = 0; i < data.vo1.length; i++) {
      var syscss = getSyscss(data.vo1[i].sysEnname);
      unableSys += '<a class="codelab-card" data-wait-for-ripple>';
      unableSys += '<div class="card-header NOPE_BG">';
      unableSys += '<div class="category-icon ' + syscss + '_ICON" style="filter: grayscale(100%)"></div>';
      unableSys += '<span class="go-text">' + data.vo1[i].sysName + '</span></div>';
      unableSys += '<div class="description">' + data.vo1[i].sysDesc + '</div>';
      unableSys += '<div class="card-footer">';
      unableSys += '<button class="goin-btn-unalbe" disabled></button>';
      if (isEmpty(data.vo1[i].sysVersion)) {
        unableSys += '<div><div class="card-updated">敬请期待</div></div>';
      } else {
        unableSys += '<div><div class="card-updated">Version ' + data.vo1[i].sysVersion + '</div></div>';
      }
      unableSys += '</div><paper-ripple></paper-ripple></a>';
    }

    $('#cards').append(availableSys);
    $('#cards').append(unableSys);
  });
}

function gotosubsys(sysAddress) {
  console.log(sysAddress);
  window.open(sysAddress);
}

function resetWinHeight() {

  var winHeight;

  if (window.innerHeight) {
    winHeight = window.innerHeight;
  } else if ((document.body) && (document.body.clientHeight)) {
    winHeight = document.body.clientHeight;
  }

  // 应用总数（每个应用对应一个a标签）
  var apps = $("card-sorter").children("a").length;

  // 没有滚动条时的高度（浏览器可视高度减去题头高度再减去脚注高度）
  var minHeight = winHeight - 68 - 40;

  // 适应应用个数后计算出的高度（距上下边高度为40px，每个小块高度200，下边距16，最后要减去一个下边距否则上下边距不一致）
  var fitHeight = 40 + 216 * Math.ceil(apps / 3) + 40 - 16;

  $('.index-body').css("height", fitHeight >= minHeight ? fitHeight : minHeight);
}

function getSyscss(sysEnname) {
  if (-1 === $.inArray(sysEnname.toLowerCase(), syscss)) {
    return "UNDEF";
  }
  return sysEnname.toUpperCase();
}